﻿using FlowSimulate.ZentaoDB;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlowSimulate {

    public class Outputer(string connectionString, string fileStore) : IDisposable {

        private static readonly ILog LOG = LogManager.GetLogger(typeof(Outputer));

        readonly ZentaoDBManager dbManager = new(connectionString);
        readonly ZentaoFileUploader uploader = new(fileStore);

        public void Dispose() {
            this.dbManager.Dispose();
            GC.SuppressFinalize(this);
        }

        public void Output(List<PersonatedTask> personatedTasks, Director director) {
            var taskDAO = this.dbManager.TaskDao;

            var pm = director.GetAssetByStageKey("PM").First();
            var pmID = pm.ID;

            foreach (var task in personatedTasks) {
                var owner = task.Asset;
                var ownerID = owner.ID;

                var title = task.Title;
                var description = this.CombineResources(task.FinishDate, task.Description, task.Resources, ownerID);

                var taskID = taskDAO.Insert(
                    title, description, 
                    ownerID, pmID, 
                    task.StartDate, task.AssignDate, task.FinishDate, task.EndDate);

                foreach (var comment in task.Comments) {
                    this.InsertComment(taskID, comment, director);
                }

                LOG.Debug($"Wrote task ID={task.ID} to zentao");
            }
        }

        private void InsertComment(int taskID, Comment comment, Director director) {
            var dao = this.dbManager.ActionDao;
            var asset = director.GetAsset(comment.User);
            var userID = asset.ID;
            if (comment.Type == CommentType.Comment) {
                dao.InsertComment(taskID, userID, comment.CreateTime, comment.Content);
            } else {
                dao.InsertAction(taskID, userID, comment.Action, comment.CreateTime, comment.Content);
            }
        }

        private string CombineResources(DateTime? time, string description, List<string> resources, string ownerID) {
            var dao = this.dbManager.FileDao;
            var date = time ?? DateTime.Parse("0000-00-00");

            var builder = new StringBuilder();
            builder.AppendLine($"<div>{description}</div>");
            
            builder.AppendLine("<div>");
            foreach (var file in resources) {
                var saveName = this.uploader.Upload(file, date);
                var fileID = dao.Insert(file, saveName, ownerID, date);
                var href = $"/www/index.php?m=file&f=read&t=png&fileID={fileID}";
                builder.AppendLine($"<img href=\"{href}\" width=\"300px\" height=\"400px\" />");
            }
            builder.AppendLine("</div>");

            return builder.ToString();
        }
    }
}